#include "UnitTest++.h"
#include "binarytree.h"

TEST(Size_should_be_three)
{
    BinaryTree<int> tree;
    int expectedSize = 3;

    tree.add(6);
    tree.add(8);
    tree.add(9);
    int size = tree.size();

    CHECK_EQUAL(expectedSize, size);
}

TEST(Should_remove_leaf_value)
{
    BinaryTree<int> tree;
    int expectedSize = 2;

    tree.add(3);
    tree.add(2);
    tree.add(5);
    tree.add(6); // 6 is a leaf

    bool removed;
    removed = tree.remove(6);
    CHECK(removed);
    removed = tree.remove(2);
    CHECK(removed);

    int size = tree.size();
    CHECK_EQUAL(expectedSize, size);
}

TEST(Should_remove_node_with_one_child)
{
    BinaryTree<int> tree;
    int expectedSize = 3;

    tree.add(2);
    tree.add(1);
    tree.add(6); // 6 has one child: 8
    tree.add(8);

    bool removed;
    removed = tree.remove(6);
    CHECK(removed);

    int size = tree.size();
    CHECK_EQUAL(expectedSize, size);
}

TEST(Should_remove_node_with_two_children)
{
    BinaryTree<int> tree;
    int expectedSize = 4;

    tree.add(3);
    tree.add(2);
    tree.add(5); // 5 has two children: 6 and 8
    tree.add(8);
    tree.add(6);

    bool removed;
    removed = tree.remove(5);
    CHECK(removed);

    int size = tree.size();
    CHECK_EQUAL(expectedSize, size);
}

TEST(Should_not_remove_value_that_doesnt_exist_in_tree)
{
    BinaryTree<int> tree;
    int expectedSize = 3;

    tree.add(3);
    tree.add(2);
    tree.add(5);

    bool removed;
    removed = tree.remove(16); // 16 does not exist in tree
    CHECK(!removed);

    int size = tree.size();
    CHECK_EQUAL(expectedSize, size);
}

TEST(Should_remove_value_in_root)
{
    BinaryTree<int> tree;
    int expectedSize = 2;

    tree.add(1); // 1 is a root value
    tree.add(2);
    tree.add(3);

    bool removed;
    removed = tree.remove(1);
    CHECK(removed);

    int size = tree.size();
    CHECK_EQUAL(expectedSize, size);
}

TEST(Should_copy_tree_in_other)
{
    BinaryTree<int> tree1;

    tree1.add(1);

    BinaryTree<int> tree2(tree1);

    tree2.add(2);

    int expectedSize = 2;
    int size = tree2.size();
    CHECK_EQUAL(expectedSize, size);
}

TEST(Original_tree_should_keep_its_values)
{
    BinaryTree<int> originalTree;
    int expectedSize = 1;

    originalTree.add(1);

    BinaryTree<int> newTree(originalTree);

    newTree.add(2);

    CHECK(newTree.remove(1));

    int size = originalTree.size();

    CHECK_EQUAL(expectedSize, size);
}

int main()
{
    UnitTest::RunAllTests();
    return 0;
}

